//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.jjc.MyWebApp.Interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.i18n.I18n;
import com.jfinal.i18n.Res;
import com.jfinal.kit.StrKit;
import com.jfinal.render.Render;
import com.jjc.MyWebApp.Constant.I18nConstant;

public class i18nInterceptor implements Interceptor {
    private String localeParaName;
    private String resName;
    private String baseName;
    private String defaultLocale;

    public i18nInterceptor() {
        this.localeParaName = I18nConstant.I18n_localePara_Name;
        this.resName = I18nConstant.I18n_Res_Name;
        this.baseName = I18nConstant.I18n_Base_Name;
        this.defaultLocale = I18nConstant.I18n_Default_Locale;
    }

    public i18nInterceptor(String localeParaName, String resName) {
        this.localeParaName = I18nConstant.I18n_localePara_Name;
        this.resName = I18nConstant.I18n_Res_Name;

        if(StrKit.isBlank(localeParaName)) {
            throw new IllegalArgumentException("localeParaName can not be blank.");
        } else if(StrKit.isBlank(resName)) {
            throw new IllegalArgumentException("resName can not be blank.");
        } else {
            this.localeParaName = localeParaName;
            this.resName = resName;
        }
    }

    public void intercept(Invocation inv) {
        Controller c = inv.getController();
        String localeParaName = this.localeParaName;
        String locale = c.getPara(localeParaName);
        if(StrKit.notBlank(locale)) {
            c.setCookie(localeParaName, locale, 999999999);
        } else {
            locale = c.getCookie(localeParaName);
            if(StrKit.isBlank(locale)) {
                locale = this.defaultLocale;
            }
        }

        Res res = I18n.use(this.baseName, locale);
        c.setAttr(this.resName, res);
        inv.invoke();


    }

}
